/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package com.moon.admin.sys.controller;

import com.moon.admin.sys.cache.RegionCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;

import com.moon.admin.sys.entity.SysRegionEntity;
import com.moon.admin.sys.domain.query.RegionQuery;
import com.moon.admin.sys.service.SysRegionService;
import com.moon.admin.framework.util.ToolUtils;
import com.moon.admin.framework.secure.annotation.FormRepeat;
import com.moon.admin.framework.secure.annotation.PreAuth;
import com.moon.admin.framework.springmvc.domain.R;
import com.moon.admin.framework.syslog.annotation.SysLog;
import com.moon.admin.framework.domain.MoonPage;
import com.moon.admin.framework.mybatisplus.query.Condition;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 行政区划 操作处理
 * 
 * @author moon
 */
@RestController
@RequestMapping("/region/sysRegion")
@Api(value="行政区划",tags="行政区划服务")
public class SysRegionController{
	
    @Autowired
    private SysRegionService sysRegionService;

    @ApiOperation(value="行政区划列表")
    @PostMapping(value = "list")
    @PreAuth("region_sysRegion_view")
    public R<MoonPage<SysRegionEntity>> list(@RequestBody RegionQuery sysRegion) {
        IPage<SysRegionEntity> pages = sysRegionService.page(Condition.getPage(sysRegion), Condition.getQueryWrapper(new SysRegionEntity(),sysRegion));
        pages.getRecords().forEach(entity->{
            entity.setParentName(RegionCache.getRegionByCode(entity.getParentCode()).getName());
        });
    	return R.success(MoonPage.of(pages));
    }

    @ApiOperation(value="行政区划添加/编辑")
    @PostMapping(value = "editSave")
    @PreAuth(value={"region_sysRegion_add","region_sysRegion_edit"})
    @SysLog(mod="行政区划服务",fun="保存")
    @FormRepeat
    public R<String> editSave(@RequestBody SysRegionEntity sysRegion){
        sysRegionService.saveOrUpdate(sysRegion);
        return R.success();
    }

    @GetMapping("/removeByIds")
    @ApiOperation(value = "行政区划", notes = "传入ids")
    @PreAuth("region_sysRegion_remove")
    @SysLog(mod="行政区划服务",fun="删除")
    @FormRepeat
    public R<Boolean> removeByIds(@ApiParam(value = "主键集合", required = true)String ids) {
        return R.status(sysRegionService.removeByIds(ToolUtils.toIntList(ids)));
    }

    @GetMapping("/cleanCache")
    @ApiIgnore
    public R<Void> cleanCache(){
        RegionCache.cleanCache();
        return R.success();
    }
}